package com.cyberweb.DAO;

import com.cyberweb.VO.PersonaAdministrador;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author OSKAR
 */
public class CyberWebDAO implements IAccesoDatos {

    @Override
    public void insertar(Object obj) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CyberWebPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(obj);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    @Override
    public void actualizar(Object obj) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CyberWebPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.merge(obj);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    @Override
    public void eliminar(Object obj) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CyberWebPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.remove(em.merge(obj));
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    @Override
    public Object consultarPorPK(Object obj) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CyberWebPU");
        EntityManager em = emf.createEntityManager();
        return em.find(PersonaAdministrador.class, obj);
    }
    
    public PersonaAdministrador consultarPorPK(String usu) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CyberWebPU");
        EntityManager em = emf.createEntityManager();
        return em.find(PersonaAdministrador.class, usu);
    }
}
